package com.bcntouch.touch2act.Opine;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.nfc.NfcAdapter;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bcntouch.touch2act.R;
import com.bcntouch.touch2act.Constants.Constants;
import com.bcntouch.touch2act.NFC.NFCUtils;
import com.bcntouch.touch2act.Suggest.SuggestActivity;

public class OpinionTagDetectedActivity extends Activity {

	Opinion mOpinion;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_opinion_tag_detected);
        
        OpinionStorage.initDefaultOpinions(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_opinion_tag_detected, menu);
        return true;
    }
    
    @Override
    public void onStop() {
    	super.onStop();
    	
    	finish();
    }
    
    @Override
    public void onResume() {
    	super.onResume();
    	
    	Intent intent = getIntent();
    	String intentAction = intent.getAction();
    	
    	// From another Activity
    	String opinionId = intent.getStringExtra(SuggestActivity.EXTRA_SUGGESTION_ID);
    	
    	if (opinionId == null) {
	    	// NFC
	    	if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intentAction)) {
	//    		String tagUid = NFCUtils.getTagUidFromIntent(intent);
	//    		Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
	    		List<Uri> uris = NFCUtils.getUris(intent);
	    		opinionId = getOpinionId(uris);
	    	}
	    	
	    	// URI
	    	else if (Intent.ACTION_VIEW.equals(intentAction)) {
	    		Uri uri = intent.getData();
	    		opinionId = getOpinionId(uri);
	    	}
    	}
    	
    	processOpinionId(opinionId);
    	
    }
    
    public void onYesButtonClick(View view) {
    	mOpinion.setHaveVoted(1);
    	mOpinion.setYourVote(1);
    	mOpinion.setNumVotes(mOpinion.getNumVotes() + 1);
    	mOpinion.setNumPositiveVotes(mOpinion.getNumPositiveVotes() + 1);
    	
    	OpinionStorage.updateOpinion(this, mOpinion);
    	
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setMessage(R.string.opinio_enviada)
    	       .setCancelable(false)
    	       .setPositiveButton(R.string.acceptar, new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	        	   	Intent intent = new Intent(OpinionTagDetectedActivity.this, SuggestActivity.class);
	    	           	startActivity(intent);
	    	           	finish();
    	           }
    	       });
    	AlertDialog alert = builder.create();
    	alert.show();
    }
    
    public void onNoButtonClick(View view) {
    	mOpinion.setHaveVoted(1);
    	mOpinion.setYourVote(0);
    	mOpinion.setNumVotes(mOpinion.getNumVotes() + 1);
    	mOpinion.setNumNegativeVotes(mOpinion.getNumNegativeVotes() + 1);
    	
    	OpinionStorage.updateOpinion(this, mOpinion);
    	
    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setMessage(R.string.opinio_enviada)
    	       .setCancelable(false)
    	       .setPositiveButton(R.string.acceptar, new DialogInterface.OnClickListener() {
    	           public void onClick(DialogInterface dialog, int id) {
    	        	   	Intent intent = new Intent(OpinionTagDetectedActivity.this, SuggestActivity.class);
	    	           	startActivity(intent);
	    	           	finish();
    	           }
    	       });
    	AlertDialog alert = builder.create();
    	alert.show();
    }
    
    public void processOpinionId(String opinionId) {
//    	Opinion opinion = Opinions.getOpinionById(this, opinionId);
    	mOpinion = OpinionStorage.getOpinionById(this, opinionId);
    	if (mOpinion == null)
    		unknownOpinion();
    	
    	TextView questionTextView = (TextView) findViewById(R.id.question_text_view);
    	questionTextView.setText(mOpinion.getQuestion());
    	
    	// Have Voted
    	if (mOpinion.getHaveVoted() > 0) {
    		Button yesButton = (Button) findViewById(R.id.yes_button);
    		Button noButton = (Button) findViewById(R.id.no_button);
    		yesButton.setHeight(0);
    		noButton.setHeight(0);
    		yesButton.setVisibility(View.GONE);
    		noButton.setVisibility(View.GONE);
    		
    		TextView alreadyAnsweredTextView = (TextView) findViewById(R.id.already_answered_text_view);
    		alreadyAnsweredTextView.setVisibility(View.VISIBLE);
    	}
    	
    	// Total votes
    	TextView totalVotesTextView = (TextView) findViewById(R.id.total_votes_text_view);
    	totalVotesTextView.setText(" " + mOpinion.getNumVotes());
    	
    	// Yes votes
    	TextView yesVotesTextView = (TextView) findViewById(R.id.yes_votes_text_view);
    	int percent = (mOpinion.getNumPositiveVotes() * 100) / mOpinion.getNumVotes();
    	yesVotesTextView.setText(" " + percent + "% (" + mOpinion.getNumPositiveVotes() + ")");
    	
    	// No votes
    	TextView noVotesTextView = (TextView) findViewById(R.id.no_votes_text_view);
    	percent = 100 - percent;
    	noVotesTextView.setText(" " + percent + "% (" + mOpinion.getNumNegativeVotes() + ")");
    }
    
    public void unknownOpinion() {
    	
    }
    
    public String getOpinionId(List<Uri> uris) {
    	String opinionId = null;
    	
    	if (uris != null) {
			for (Uri uri: uris) {
				opinionId = getOpinionId(uri);
			}
		}
    	
    	return opinionId;
    }
    
    public String getOpinionId(Uri uri) {
    	if (uri == null)
    		return null;
    	
    	String opinionId = null;
    	
    	String incomingHost = uri.getHost();
    	if (Constants.APP_HOST.equals(incomingHost)) {
    		List<String> pathSegments = uri.getPathSegments();
    		if (pathSegments == null || pathSegments.isEmpty())
    			return null;
    		
    		String pathSegment = pathSegments.get(0);
    		if (Constants.PATH_OPINION.equals(pathSegment)) {
    			opinionId = uri.getQueryParameter(Constants.PAR_OPINION_ID);
    		}
    	}
    	
    	return opinionId;
    }
}
